package com.kingsalert.kingsalert.view.process_widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseProcessWidgetManager;
import com.kingsalert.kingsalert.bean.process.ProcessInfo_Data;
import com.kingsalert.kingsalert.bean.userinfo.AccountList_Fr;
import com.kingsalert.kingsalert.bean.userinfo.BankAccount_Fr;
import com.kingsalert.kingsalert.config.CountryConfig;
import com.kingsalert.kingsalert.dialog.BindAccountDialog_Cote2;
import com.kingsalert.kingsalert.dialog.BindAccountDialog_CoteWave;
import com.kingsalert.kingsalert.dialog.BindAccountDialog_Fr;
import com.kingsalert.kingsalert.dialog.DeleteAccountDialog_Fr;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAccountViewManager_Fr extends BaseProcessWidgetManager {

    private final FragmentActivity activity;

    private LinearLayout groupLayout;

    private ArrayList<BankAccount_Fr> accounts = new ArrayList<>();

    private boolean guide = true;

    public BindAccountViewManager_Fr(View view, ProcessInfo_Data data,String pageTag){
        super(view,data,pageTag);
        activity = (FragmentActivity) view.getContext();
     }

    @Override
    protected void init(){
        groupLayout = mView.findViewById(R.id.vpbf_layout_group);
        View addView = mView.findViewById(R.id.vpbf_view_add);

        if (CountryConfig.getInstance().isCote()) addView.setOnClickListener(v -> coteBindAccountGuide());
        else addView.setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"ADD_CLICK","");
            new BindAccountDialog_Fr(activity, page,
                    () -> HttpUtil.getInstance().requestAccountList_Fr(activity,this::refreshAccountList)
            ).show();
        });
    }

    @Override
    public boolean checkInput() {
        return !accounts.isEmpty();
    }

    @Override
    public String getInput() {
        return String.valueOf(accounts.size());
    }

    //科特绑卡引导
    public void coteBindAccountGuide(){
        if (accounts.isEmpty())//科特Wave引导绑卡
            new BindAccountDialog_CoteWave(activity, page,
                    () -> HttpUtil.getInstance().requestAccountList_Fr(activity,this::refreshAccountList)
            ).show();
        else
            new BindAccountDialog_Cote2(activity, page,
                () -> HttpUtil.getInstance().requestAccountList_Fr(activity,this::refreshAccountList)
            ).show();
    }

    //更新列表
    public void refreshAccountList(){
        groupLayout.removeAllViews();

        accounts = AccountList_Fr.getInstance().getBankAccountList();

        final boolean deleteAble = accounts.size()>1;

        for (BankAccount_Fr account:accounts){
            addAccount(account,deleteAble);
        }

        //科特首次引导绑卡
        if (CountryConfig.getInstance().isCote()&&guide) {
            coteBindAccountGuide();
            guide = false;
        }
    }

    //添加卡
    private void addAccount(BankAccount_Fr account,boolean deleteable){
        if (account == null) return;

        View accountView = View.inflate(mView.getContext(), R.layout.item_fr_account, null);
        TextView accountmobileTv = accountView.findViewById(R.id.ifa_tv_account_mobile);
        ImageView accountlogoIv = accountView.findViewById(R.id.ifa_iv_logo);
        TextView accountchannelTv = accountView.findViewById(R.id.ifa_tv_account_channel);
        ImageButton deleteaccountIb = accountView.findViewById(R.id.ifa_ib_deleteAccount);

        accountmobileTv.setText(account.getWalletMobile());
        accountchannelTv.setText(account.getWalletDesc());

        //删除卡
        deleteaccountIb.setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"DELETE_CLICK",account.getWalletMobile());
            new DeleteAccountDialog_Fr(activity,account, page,
                    () -> HttpUtil.getInstance().requestAccountList_Fr(activity,this::refreshAccountList)).show();
        });

        deleteaccountIb.setVisibility(deleteable ? View.VISIBLE:View.GONE);

        groupLayout.addView(accountView);

        //获取图标
        if (account.getBitmap()==null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo())) return;
                    URL url = new URL(account.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        account.setBitmap(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else accountlogoIv.setImageBitmap(account.getBitmap());
    }

}
